<?php
class User extends  CS_Controller
{
    public function _init()
    {
        $this->load->model('tourism_order_model', 'tourism_order');
        $this->load->model('tourism_order_product_model', 'tourism_order_product');
        $this->load->model('tourism_delivery_address_model', 'tourism_delivery_address');
        $this->load->model('tourism_goods_attr_model', 'tourism_goods_attr');
        $this->load->model('tourism_order_refund_model', 'tourism_order_refund');
        $this->load->model('tourism_goods_base_model', 'tourism_goods_base');
        $this->load->model('express_model', 'express');
        $this->load->model('express_record_model', 'express_record');
        $this->load->helper('common');
    }

    public function orderList()
    {
        $history = get_cookie('historyOrder');
        if (!empty($history)) {
            $this->data['historyOrder'] = json_decode($history);
        }
        $this->data['localSpecial'] = $this->tourism_goods_base->findLocalSpecial(6, ['uid' => $this->providerUid])->result();  //特产推荐
        $this->load->view('tourism/user/list', $this->data);
    }

    public function orderSearch()
    {
        $keyword = $this->input->get('keyword');
        $history = get_cookie('historyOrder');
        if (!empty($keyword)) {
            $historyOrder = array();
            if (empty($history)) {
                $historyOrder[] = $keyword;
            } else {
                $historyOrder = json_decode($history);
                if(!in_array($keyword,$historyOrder)){
                    array_unshift($historyOrder, $keyword);
                }
            }
            set_cookie('historyOrder', json_encode($historyOrder), 86500);
        }

        $this->load->view('tourism/user/ordersearch', $this->data);
    }

    public function deleteCookie()
    {
        delete_cookie('historyOrder');

        echo json_encode(array(
            'status' => true
        ));exit;
    }

    public function ajaxOrderList()
    {
        $state = $this->input->get('status');
        $start = $this->input->get('start');
        $count = $this->input->get('count');
        $keyword = $this->input->get('keyword');
        $total = $this->tourism_order->total($this->uid,$state, $keyword);
        $result = $this->tourism_order->findByUid($this->uid,$state,$count,$start, $keyword);
//        echo $this->db->last_query();
        $data['list'] = $this->decrypt($result);
        echo json_encode(
            array(
                'total'  => $total,
                'html'   => $this->load->view('tourism/user/ajaxorderlist', $data, true)
            )
        );
        exit;
    }

    public function decrypt($data)
    {
        $argc = array();
        if ($data->num_rows() <= 0) {
            return array();
        }
        foreach ($data->result() as $key => $item) {
            $argc[$item->order_id]['alias_name'] = $item->alias_name;
            $argc[$item->order_id]['order_id'] = $item->order_id;
            $argc[$item->order_id]['order_sn'] = $item->order_sn;
            $argc[$item->order_id]['order_pay_total'] = $item->order_pay_total;
            $argc[$item->order_id]['transport_cost'] = $item->transport_cost;
            $argc[$item->order_id]['order_state'] = $item->order_state;
            $argc[$item->order_id]['order_status'] = $item->order_status;
            $argc[$item->order_id]['shop_goods'][] = $item;
        }
        return $argc;
    }

    public function cancelOrder($orderId)
    {
        $orderInfo = $this->isExistOrder($orderId);

        if ($orderInfo->order_status != 2 || $orderInfo->order_state != 1) {
            $this->alertJumpPre('取消失败,订单状态有误');
        }

        $this->db->trans_start();
        $stock = $this->tourism_order_product->findStock($orderId);
        //订单状态修改
        $this->tourism_order->updateByOrderId($orderId, array('order_status'=>1));
        //库存返还
        foreach ($stock->result() as $item) {
            $productParams['goods_attr_id'] = $item->goods_attr_id;
            $productParams['goods_num'] = $item->refund_num;
            $this->tourism_goods_attr->updateGoodsNum($productParams);
        }
        $this->db->trans_complete();

        if($this->db->trans_status() === FALSE){
            $this->alertJumpPre('取消订单失败');
        }else{
            $this->success('tourism/user/orderList','','取消订单成功');
        }
    }

    //确认收货
    public function confirmReceipt($orderId)
    {
        $orderInfo = $this->isExistOrder($orderId);
        if($orderInfo->order_state != 2 || $orderInfo->order_status != 4){
            $this->jsonMessage('订单状态有误');
        }
        $param['order_state'] = 3;
        $param['order_status'] = 5;
        $param['receive_time'] = date('Y-m-d H:i:s');
        $result = $this->tourism_order->updateByOrderId($orderId, $param);

        if(!$result){
            $this->alertJumpPre('取消订单失败');
        }else{
            $this->success('tourism/user/orderList','','取消订单成功');
        }
    }

    public function orderDetail($orderId)
    {
        $orderInfo = $this->isExistOrder($orderId);

        $result = $this->tourism_order_product->findRefundOrder($orderId);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('该订单不存在');
        }
        $this->data['orderList'] = $result->result();

        $this->data['orderInfo'] = $orderInfo;
        $logisticsInfo = $this->express_record->findByNu($orderInfo->logistics_company, $orderInfo->logistics);
        if ($logisticsInfo->num_rows() > 0) {
            $this->data['logisticsInfo'] = $logisticsInfo->row();
        }
        $this->load->view('tourism/user/detail', $this->data);
    }

    private function isExistOrder($orderId)
    {
        $result = $this->tourism_order->findAliasName($orderId);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('该订单不存在');
        }
        return $result->row();
    }

    public function tracking($orderId)
    {
       $orderInfo = $this->isExistOrder($orderId);
        $this->data['logisticsInfo'] = $this->express_record->findByNu($orderInfo->logistics_company, $orderInfo->logistics)->row();
        $this->data['logistics'] = $this->express->findAll();
        $this->load->view('tourism/user/tracking', $this->data);
    }
}